import { defineStore } from 'pinia'
import { getDetailInfo ,addCartInfo} from '@/api/detail'
export const useDetailStore = defineStore('useDetailStore', {
  state() {
    return {
      detailInfo: {},
    }
  },
  //   请求
  actions: {
    //获取商品详情
   async getDetailInfoHandler(proid:string) {
        const result =await getDetailInfo(proid)
        if ((result as any).code == 200) {
            //返回一个成功的promise
            this.detailInfo =result.data
            return Promise.resolve("成功!")
          } else {
            //返回一个成功的promise
            return Promise.reject("失败!")
          }
   },
  //  购物车添加
   async addCartInfoHandler(params:object) {
        const result:any =await addCartInfo(params)
          if (result.code == 200) {
            // 成功返回的数据
            return Promise.resolve(result.message)
          } else {
            return Promise.reject("失败!")
          }
   },
  },
  getters: {
    //使用pinia里面的计算属性实现数据加工
    detailInfoMsg():any {
      if (JSON.stringify(this.detailInfo) != '{}') {//如果对象当中存在数据   
        return {
          banners: (this.detailInfo as any).banners[0].split(','),
          originprice: (this.detailInfo as any).originprice ,
          proname: (this.detailInfo as any).proname
        }
      }
    }
  }
  
})
